<?php

require_once('include_config_layout.php');
require_once('class_login.php');

class Layout {

  /* An array of urls of stylesheets to be included on the page */
  protected $stylesheets;

  /* An array of urls of javascript files to be included on the page */
  protected $javascripts;

  /* CSS to be displayed inline with the page header */
  protected $inlineCss;

  /* Javascript to be displayed inline with the page header */
  protected $inlineJavascript;

  /* An associative array of url=>title pairs of breadcrumbs */
  protected $breadcrumbs = array();

  /* When set, this overrides the page title in the breadcrumb string */
  public $lastBreadcrumb;

  /* Set to true to prevent the breadcrumbs from being displayed */
  public $hideBreadcrumbs = false;

  /* A list of links to be made into buttons at the top right of the page */
  public $topRightButtons = array();

  /* Set to true to prevent the title from being displayed */
  public $hideTitle = false;

  /* The URL of a page to redirect to via META-REFRESH */
  public $redirect;

  /* The amount of time to wait before redirecting */
  public $wait = 2;

  /* A javascript function to call once the page has loaded */
  public $onload;

  /* An associative array of configuration options from the config file */
  public $config;

  /* The title of the current page */
  public $title;

  /* Constructor */
  public function __construct($title = 'Default Title')
  {
    $this->title = $title;

    global $layoutConfig;
    $this->config = $layoutConfig;

    foreach ($this->config['DEFAULT_STYLESHEETS'] as $stylesheet) {
      $this->attachStylesheet($stylesheet);
    }
    foreach ($this->config['DEFAULT_JAVASCRIPTS'] as $javascript) {
      $this->attachJavascript($javascript);
    }
  }

  /* Add a stylesheet URL to the list of stylesheets for this page */
  public function attachStylesheet($stylesheet)
  {
    if (strpos($stylesheet, '/') === false) {
      $this->stylesheets[] = $this->config['DEFAULT_STYLESHEET_PATH'] .
          $stylesheet;
    } else {
      $this->stylesheets[] = $stylesheet;
    }
  }

  /* Add a javascript file URL to the list of javascript files for this page */
  public function attachJavascript($javascript)
  {
    if (strpos($javascript, '/') === false) {
      $this->javascripts[] = $this->config['DEFAULT_JAVASCRIPT_PATH'] .
          $javascript;
    } else {
      $this->javascripts[] = $javascript;
    }
  }

  public function setInlineCss($css)
  {
    $this->inlineCss = $css;
  }

  public function setInlineJavaScript($script)
  {
    $this->inlineJavascript = $script;
  }

  public function addBreadcrumb($title, $url)
  {
    $this->breadcrumbs[] = new Link($title, $url);
  }

  public function addTopRightButton($title, $url)
  {
    $this->topRightButtons[] = new Link($title, $url);
  }

  /* Show the header */
  public function showHeader()
  {
    ob_start();
    echo "{$this->config['DOCTYPE']}\n";
    echo "{$this->config['HTML_ELEMENT']}\n";
    echo "<head>\n";
    echo "{$this->config['CONTENT_TYPE']}\n";
    $this->showTitleTag();
    $this->showFavicon();
    $this->showCss();
    $this->showJavascript();
    $this->showRedirect();
    echo "</head>\n";
    $this->showBodyTag();
    $this->showContainer();
    $this->includeHeader();
    $this->showSessionMessage();
    $this->showBreadcrumbs();
    $this->showTopRightButtons();
    $this->showPageTitle();
    ob_end_flush();
  }

  protected function showTitleTag()
  {
    echo "<title>$this->title - {$this->config['SITE_TITLE']}</title>\n";
  }

  protected function showFavicon()
  {
    echo "<link rel=\"shortcut icon\" href=\"/favicon.ico\" " .
        "type=\"image/x-icon\" />\n";
  }

  protected function showCss()
  {
    foreach ($this->stylesheets as $sheet) {
      echo "<link href=\"$sheet\" rel=\"stylesheet\" type=\"text/css\" />\n";
    }
    if (isset($this->inlineCss)) {
      echo "<style type=\"text/css\">$this->inlineCss</style>\n";
    }
  }

  protected function showJavascript()
  {
    foreach ($this->javascripts as $script) {
      echo "<script src=\"$script\" type=\"text/javascript\"></script>\n";
    }
    if (isset($this->inlineJavascript)) {
      echo "<script type=\"text/javascript\">$this->inlineJavascript" .
          "</script>\n";
    }
  }

  protected function showRedirect()
  {
    if (isset($this->redirect)) {
      echo "<meta http-equiv=\"refresh\" " .
          "content=\"$this->wait;url=$this->redirect\" />";
    }
  }

  protected function showBodyTag()
  {
    if (isset($this->onload)) {
      echo "<body onload=\"$this->onload\">\n";
    } else {
      echo "<body>\n";
    }
  }

  protected function showContainer()
  {
    echo "<div id=\"{$this->config['CSS_CONTAINER_ID']}\">\n";
  }

  protected function includeHeader()
  {
    require_once ($this->config['INCLUDE_HEADER']);
  }

  protected function showSessionMessage()
  {
    $message = Util::getSessionMessage();
    if ($message) {
      echo "<div id=\"{$this->config['CSS_MESSAGE']}\">$message</div>\n";
      Util::unsetSessionMessage();
    }
  }

  protected function showBreadcrumbs()
  {
    if (!$this->hideBreadcrumbs) {
      echo "<div id=\"{$this->config['CSS_BREADCRUMBS_ID']}\">\n";
      echo "<a href=\"/\">Home</a>\n";
      foreach ($this->breadcrumbs as $breadcrumb) {
        if ($breadcrumb->url) {
          echo " > <a href=\"$breadcrumb->url\">$breadcrumb->title</a>\n";
        } else {
          echo " > $breadcrumb->title\n";
        }
      }
      if (isset($this->lastBreadcrumb)) {
        echo " > $this->lastBreadcrumb\n";
      } else {
        echo " > $this->title\n";
      }
      echo "</div>\n";
    }
  }

  protected function showTopRightButtons()
  {
    // TODO: Think of a way to refactor this.
    if (count($this->topRightButtons) > 0) {
      echo "<div id=\"top-right-buttons\">\n";
      foreach ($this->topRightButtons as $button) {
        echo "<div class=\"top-right-button\">";
        echo "<a href=\"$button->url\">$button->title</a>";
        echo "</div>\n";
      }
      echo "</div>\n";
    }
  }

  protected function showPageTitle()
  {
    if (!$this->hideTitle) {
      echo "<h1>{$this->title}</h1>\n";
    }
  }

  public function showFooter()
  {
    require_once ($this->config['INCLUDE_FOOTER']);
    $html[] = '</div>';
    $html[] = '</body>';
    $html[] = '</html>';
    echo implode("\n", $html) . "\n";
  }
}

class Link
{
  public $title;
  public $url;

  public function __construct($title, $url)
  {
    $this->title = $title;
    $this->url = $url;
  }
}

?>